﻿/*
 * Almathy Center - VcardWindow.xaml.cs
 * Copyright (C) 2011 Christophe LEMOINE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * */
using System.Windows;
using System.Windows.Media.Imaging;

namespace AlmathyCenter
{
    /// <summary>
    /// Logique d'interaction pour VcardWindow.xaml
    /// </summary>
    public partial class VcardWindow : Window
    {

        private bool _isEdit = false;
        private bool _deleteAvatar = false;
        public bool updated = false;

        private Datas.Vcard _Vcard = null;
        public Datas.Vcard Vcard
        {
            get { return _Vcard; }
            set 
           { 
                _Vcard = value;
                UpdateContent(); 
            }
        }

        private agsXMPP.Jid _Jid = null;
        public agsXMPP.Jid Jid
        {
            get { return _Jid; }
        }

        public VcardWindow(agsXMPP.Jid jid, Datas.Vcard vcard, bool isEdit)
        {
            InitializeComponent();
            this.DataContext = this;
            _isEdit = isEdit;
            _Jid = jid;
            Vcard = vcard;
        }

        private void UpdateContent()
        {
            this.Left = (SystemParameters.PrimaryScreenWidth / 2) - (this.Width / 2);
            this.Top = (SystemParameters.PrimaryScreenHeight / 2) - (this.Height / 2);
            textBlock1.Visibility = (_isEdit) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
            content1.IsEnabled = content2.IsEnabled = content3.IsEnabled = content4.IsEnabled = button3.IsEnabled = _isEdit;
            textBox1.Text = Vcard.Fullname;
            textBox2.Text = Vcard.Nickname;
            textBox3.Text = Vcard.Name.Family;
            textBox4.Text = Vcard.Name.Given;
            textBox5.Text = Vcard.Name.Prefix;
            textBox6.Text = Vcard.Name.Middle;
            textBox7.Text = (Vcard.HomeAddress != null && Vcard.HomeAddress.Street != null) ? Vcard.HomeAddress.Street : string.Empty;
            textBox8.Text = (Vcard.HomeAddress != null && Vcard.HomeAddress.ExtraAddress != null) ? Vcard.HomeAddress.ExtraAddress : string.Empty;
            textBox9.Text = (Vcard.HomeAddress != null && Vcard.HomeAddress.PostalCode != null) ? Vcard.HomeAddress.PostalCode : string.Empty;
            textBox10.Text = (Vcard.HomeAddress != null && Vcard.HomeAddress.Locality != null) ? Vcard.HomeAddress.Locality : string.Empty;
            textBox11.Text = (Vcard.HomeAddress != null && Vcard.HomeAddress.Region != null) ? Vcard.HomeAddress.Region : string.Empty;
            textBox12.Text = (Vcard.HomeAddress != null && Vcard.HomeAddress.Country != null) ? Vcard.HomeAddress.Country : string.Empty;
            textBox13.Text = (Vcard.HomePhones[agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER] != null && Vcard.HomePhones[agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER].Number != null) ? Vcard.HomePhones[agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER].Number : string.Empty;
            textBox14.Text = (Vcard.HomePhones[agsXMPP.protocol.iq.vcard.TelephoneType.FAX] != null && Vcard.HomePhones[agsXMPP.protocol.iq.vcard.TelephoneType.FAX].Number != null) ? Vcard.HomePhones[agsXMPP.protocol.iq.vcard.TelephoneType.FAX].Number : string.Empty;
            textBox15.Text = (Vcard.HomePhones[agsXMPP.protocol.iq.vcard.TelephoneType.MSG] != null && Vcard.HomePhones[agsXMPP.protocol.iq.vcard.TelephoneType.MSG].Number != null) ? Vcard.HomePhones[agsXMPP.protocol.iq.vcard.TelephoneType.MSG].Number : string.Empty;
            textBox35.Text = (Vcard.HomeEmail != null && Vcard.HomeEmail.UserId != null) ? Vcard.HomeEmail.UserId : string.Empty;
            textBox17.Text = (Vcard.Organization != null && Vcard.Organization.Name != null) ? Vcard.Organization.Name : string.Empty;
            textBox18.Text = (Vcard.Organization != null && Vcard.Organization.Unit != null) ? Vcard.Organization.Unit : string.Empty;
            textBox19.Text = Vcard.Title;
            textBox20.Text = Vcard.Role;
            textBox21.Text = (Vcard.WorkAddress != null && Vcard.WorkAddress.Street != null) ? Vcard.WorkAddress.Street : string.Empty;
            textBox22.Text = (Vcard.WorkAddress != null && Vcard.WorkAddress.ExtraAddress != null) ? Vcard.WorkAddress.ExtraAddress : string.Empty;
            textBox23.Text = (Vcard.WorkAddress != null && Vcard.WorkAddress.PostalCode != null) ? Vcard.WorkAddress.PostalCode : string.Empty;
            textBox24.Text = (Vcard.WorkAddress != null && Vcard.WorkAddress.Locality != null) ? Vcard.WorkAddress.Locality : string.Empty;
            textBox25.Text = (Vcard.WorkAddress != null && Vcard.WorkAddress.Region != null) ? Vcard.WorkAddress.Region : string.Empty;
            textBox26.Text = (Vcard.WorkAddress != null && Vcard.WorkAddress.Country != null) ? Vcard.WorkAddress.Country : string.Empty;
            textBox27.Text = (Vcard.WorkPhones[agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER] != null && Vcard.WorkPhones[agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER].Number != null) ? Vcard.WorkPhones[agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER].Number : string.Empty;
            textBox28.Text = (Vcard.WorkPhones[agsXMPP.protocol.iq.vcard.TelephoneType.FAX] != null && Vcard.WorkPhones[agsXMPP.protocol.iq.vcard.TelephoneType.FAX].Number != null) ? Vcard.WorkPhones[agsXMPP.protocol.iq.vcard.TelephoneType.FAX].Number : string.Empty;
            textBox29.Text = (Vcard.WorkPhones[agsXMPP.protocol.iq.vcard.TelephoneType.MSG] != null && Vcard.WorkPhones[agsXMPP.protocol.iq.vcard.TelephoneType.MSG].Number != null) ? Vcard.WorkPhones[agsXMPP.protocol.iq.vcard.TelephoneType.MSG].Number : string.Empty;
            textBox36.Text = (Vcard.WorkEmail != null && Vcard.WorkEmail.UserId != null) ? Vcard.WorkEmail.UserId : string.Empty;
            textBox30.Text = Vcard.Url;
            dateTimePicker1.DisplayDate = Vcard.Birthday;
            dateTimePicker1.SelectedDate = Vcard.Birthday;
            textBox32.Text = Vcard.Description;
            label34.Content = string.Empty;
            button2.IsEnabled = false;
            photo1.Tag = string.Empty;
            _deleteAvatar = false;
            if (Vcard.Photo != null && Vcard.Photo.Image != null)
            {
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                Vcard.Photo.Image.Save(ms, Vcard.Photo.ImageFormat);
                ms.Seek(0, System.IO.SeekOrigin.Begin);
                bi.StreamSource = ms;
                bi.EndInit();
                photo1.Source = bi;
                label34.Content = Vcard.Photo.ImageFormat.ToString() + ", " + ((int)System.Math.Floor(bi.Width)).ToString() + "px x " + ((int)System.Math.Floor(bi.Height)).ToString() + "px";
                button2.IsEnabled = true;
            }
            updated = false;
        }

        private void expander1_Expanded(object sender, RoutedEventArgs e)
        {
            if(expander2 != null)
                expander2.IsExpanded = false;

            if(expander3 != null)
                expander3.IsExpanded = false;

            if(expander4 != null)
                expander4.IsExpanded = false;
        }

        private void expander2_Expanded(object sender, RoutedEventArgs e)
        {
            if (expander1 != null)
                expander1.IsExpanded = false;

            if (expander3 != null)
                expander3.IsExpanded = false;

            if (expander4 != null)
                expander4.IsExpanded = false;
        }

        private void expander3_Expanded(object sender, RoutedEventArgs e)
        {
            if (expander2 != null)
                expander2.IsExpanded = false;

            if (expander1 != null)
                expander1.IsExpanded = false;

            if (expander4 != null)
                expander4.IsExpanded = false;
        }

        private void expander4_Expanded(object sender, RoutedEventArgs e)
        {
            if (expander2 != null)
                expander2.IsExpanded = false;

            if (expander3 != null)
                expander3.IsExpanded = false;

            if (expander1 != null)
                expander1.IsExpanded = false;
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            expander1.IsExpanded = false;
            expander2.IsExpanded = false;
            expander3.IsExpanded = false;
            expander4.IsExpanded = false;
            Vcard.Fullname = textBox1.Text;
            Vcard.Nickname = textBox2.Text;
            Vcard.Name = new agsXMPP.protocol.iq.vcard.Name(textBox3.Text.Trim(), textBox4.Text.Trim(), textBox6.Text.Trim());
            Vcard.HomeAddress = new agsXMPP.protocol.iq.vcard.Address(agsXMPP.protocol.iq.vcard.AddressLocation.HOME, textBox8.Text.Trim(), textBox7.Text.Trim(), textBox10.Text.Trim(), textBox11.Text.Trim(), textBox9.Text.Trim(), textBox12.Text.Trim(), false);
            Vcard.HomePhones = new System.Collections.Generic.Dictionary<agsXMPP.protocol.iq.vcard.TelephoneType, agsXMPP.protocol.iq.vcard.Telephone>();
            Vcard.HomePhones.Add(agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER, new agsXMPP.protocol.iq.vcard.Telephone(agsXMPP.protocol.iq.vcard.TelephoneLocation.HOME, agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER, textBox13.Text.Trim()));
            Vcard.HomePhones.Add(agsXMPP.protocol.iq.vcard.TelephoneType.FAX, new agsXMPP.protocol.iq.vcard.Telephone(agsXMPP.protocol.iq.vcard.TelephoneLocation.HOME, agsXMPP.protocol.iq.vcard.TelephoneType.FAX, textBox14.Text.Trim()));
            Vcard.HomePhones.Add(agsXMPP.protocol.iq.vcard.TelephoneType.MSG, new agsXMPP.protocol.iq.vcard.Telephone(agsXMPP.protocol.iq.vcard.TelephoneLocation.HOME, agsXMPP.protocol.iq.vcard.TelephoneType.MSG, textBox15.Text.Trim()));
            Vcard.HomeEmail = new agsXMPP.protocol.iq.vcard.Email(agsXMPP.protocol.iq.vcard.EmailType.HOME, textBox35.Text.Trim(), false);
            Vcard.Organization = new agsXMPP.protocol.iq.vcard.Organization(textBox17.Text.Trim(), textBox18.Text.Trim());
            Vcard.Title = textBox19.Text;
            Vcard.Role = textBox20.Text;
            Vcard.WorkAddress = new agsXMPP.protocol.iq.vcard.Address(agsXMPP.protocol.iq.vcard.AddressLocation.WORK, textBox22.Text.Trim(), textBox21.Text.Trim(), textBox24.Text.Trim(), textBox25.Text.Trim(), textBox23.Text.Trim(), textBox26.Text.Trim(), false);
            Vcard.WorkPhones = new System.Collections.Generic.Dictionary<agsXMPP.protocol.iq.vcard.TelephoneType, agsXMPP.protocol.iq.vcard.Telephone>();
            Vcard.WorkPhones.Add(agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER, new agsXMPP.protocol.iq.vcard.Telephone(agsXMPP.protocol.iq.vcard.TelephoneLocation.WORK, agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER, textBox27.Text.Trim()));
            Vcard.WorkPhones.Add(agsXMPP.protocol.iq.vcard.TelephoneType.FAX, new agsXMPP.protocol.iq.vcard.Telephone(agsXMPP.protocol.iq.vcard.TelephoneLocation.WORK, agsXMPP.protocol.iq.vcard.TelephoneType.FAX, textBox28.Text.Trim()));
            Vcard.WorkPhones.Add(agsXMPP.protocol.iq.vcard.TelephoneType.MSG, new agsXMPP.protocol.iq.vcard.Telephone(agsXMPP.protocol.iq.vcard.TelephoneLocation.WORK, agsXMPP.protocol.iq.vcard.TelephoneType.MSG, textBox29.Text.Trim()));
            Vcard.WorkEmail = new agsXMPP.protocol.iq.vcard.Email(agsXMPP.protocol.iq.vcard.EmailType.WORK, textBox36.Text.Trim(), false);
            Vcard.Url = textBox30.Text;
            Vcard.Birthday = (System.DateTime)dateTimePicker1.SelectedDate;
            Vcard.Description = textBox32.Text;
            if (photo1.Tag != null && ((string)photo1.Tag) != string.Empty)
            {
                if (System.IO.File.Exists(((string)photo1.Tag)))
                {
                    try
                    {
                        System.Drawing.Image im = Vcard.PrepareImageFile(((string)photo1.Tag), 96, 96, true);
                        System.IO.MemoryStream ms = new System.IO.MemoryStream();
                        im.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        ms.Seek(0, System.IO.SeekOrigin.Begin);
                        im = System.Drawing.Image.FromStream(ms);
                        Vcard.Photo = new agsXMPP.protocol.iq.vcard.Photo(im, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    catch
                    {
                        textBox33.Text = string.Empty;
                        label34.Content = string.Empty;
                        button2.IsEnabled = false;
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.UriSource = new System.Uri("/AlmathyCenter;component/Resources/photo.png", System.UriKind.RelativeOrAbsolute);
                        bi.EndInit();
                        photo1.Source = bi;
                        photo1.Tag = string.Empty;
                        Vcard.Photo = null;
                    }
                }
            }
            if (_deleteAvatar)
                Vcard.Photo = null;

            this.DialogResult = true;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBox33.Text = string.Empty;
            label34.Content = string.Empty;
            button2.IsEnabled = false;
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new System.Uri("/AlmathyCenter;component/Resources/photo.png", System.UriKind.RelativeOrAbsolute);
            bi.EndInit();
            photo1.Source = bi;
            photo1.Tag = string.Empty;
            _deleteAvatar = true;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.AddExtension = false;
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            ofd.CustomPlaces.Add(new Microsoft.Win32.FileDialogCustomPlace(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)));
            ofd.CustomPlaces.Add(new Microsoft.Win32.FileDialogCustomPlace(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures)));
            ofd.CustomPlaces.Add(new Microsoft.Win32.FileDialogCustomPlace(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)));
            ofd.Filter = "Tous les fichiers images|*.jpg;*.jpeg;*.png;*.bmp";
            ofd.FilterIndex = 0;
            ofd.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            ofd.Multiselect = false;
            ofd.ReadOnlyChecked = true;
            ofd.ShowReadOnly = false;
            ofd.Title = "Choisissez votre avatar";
            ofd.ValidateNames = true;
            if ((bool)ofd.ShowDialog())
            {
                try
                {
                    System.Drawing.Image im = Vcard.PrepareImageFile(ofd.FileName, 96, 96, true);
                    if (im.Width < 32 || im.Height < 32)
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("L'image doit posséder au moins 32 pixels en largeur et 32 pixels en hauteur.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        System.IO.MemoryStream ms = new System.IO.MemoryStream();
                        im.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        ms.Seek(0, System.IO.SeekOrigin.Begin);
                        bi.StreamSource = ms;
                        bi.EndInit();
                        photo1.Source = bi;
                        textBox33.Text = ofd.FileName;
                        label34.Content = "Png, " + ((int)System.Math.Floor(bi.Width)).ToString() + "px x " + ((int)System.Math.Floor(bi.Height)).ToString() + "px";
                        button2.IsEnabled = true;
                        photo1.Tag = ofd.FileName;
                        _deleteAvatar = false;
                    }
                }
                catch
                {
                    Microsoft.Windows.Controls.MessageBox.Show("Impossible de charger cette image!", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        #region Update events

        private void textBox1_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox2_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox3_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox4_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox5_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox6_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox7_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox8_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox9_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox10_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox11_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox12_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox13_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox14_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox15_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox35_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox17_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox18_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox19_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox20_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox21_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox22_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox23_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox24_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox25_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox26_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox27_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox28_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox29_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox36_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox30_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void dateTimePicker1_SelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox32_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        private void textBox33_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            updated = true;
        }

        #endregion

    }

}
